*****************************************************************************
* FIGURE D1 IN APPENDIX
*****************************************************************************
use "$datapath/__EPFRdata__4__Main__Results.dta", clear
keep datem fundid country  Wint
drop if country == "unitedstates" // not included in Bertaut Tryon as they study US equity investments made in foreign countries
collapse (sum) Wnt = Wint, by(country datem) // sum across funds by country and months
bys datem : egen Wt = total(Wnt), m

* merge with Bertaut and Tryon data restated to 2016
merge 1:1 country datem using "$datapath/BT_restatedflows1994-2016", keepusing(stk_bt_pos) // equity amount invested in each foreign country
drop if datem < ym(2002,2)
drop if datem > ym(2016,7)
run "$buildpath/0_countryselection" // 35 countries

rename stk_bt_pos WBTnt
bys datem : egen WBTt = total(WBTnt), m

* keep key variables 
keep datem country Wnt WBTnt Wt WBTt

* dummy for geography
gen geo = "Asia" if country == "china" | country == "hongkong" ///
| country == "india" | country == "indonesia" | country == "japan" ///
| country == "koreasouth" | country == "malaysia" ///
| country == "australia" ///
| country == "philippines" | country == "singapore" ///
| country == "taiwan" | country == "thailand"

replace geo = "MENA" if country == "israel" | country == "turkey"

replace geo = "Africa" if country == "southafrica" 

replace geo = "NA" if country == "canada" | country == "unitedstates"

replace geo = "LatAm" if country == "brazil" | country == "chile" | country == "colombia" ///
| country == "mexico" | country == "peru" 

replace geo = "Europe" if country == "belgium" | country == "denmark" | country == "finland" | country == "france" ///
| country == "germany" | country == "ireland" | country == "italy" ///
| country == "netherlands" | country == "norway" ///
| country == "poland" | country == "spain"   ///
| country == "sweden" | country == "switzerland" | country == "unitedkingdom" 

*****************************************************************************
* 							Assets by region
*****************************************************************************

* total equity position across month and region
bys datem geo: egen WR = total(Wnt), m
bys datem geo: egen WRBT = total(WBTnt), m

* regional portfolio share
gen zR = 100*WR/Wt
gen zRBT = 100*WRBT/WBTt

local doit = 1
if `doit' == 1 {
	preserve
			
		collapse (mean) zR* , by(geo datem)
		
			twoway (line zRBT datem if geo == "Asia",  lcolor(gs5)) (line zR datem if geo == "Asia", yaxis(2) lcolor(black) lpattern(shortdash)), graphregion(color(white)) legend(off) xtitle(" " "Months")  ytitle("Percent" " ") ytitle(" " "Percent", axis(2)) 
			graph export "$finalpath/figures/asset_Asia.pdf", replace
			
			twoway (line zRBT datem if geo == "LatAm",  lcolor(gs5)) (line zR datem if geo == "LatAm", yaxis(2) lcolor(black) lpattern(shortdash)), graphregion(color(white)) legend(off) xtitle(" " "Months")  ytitle("Percent" " ") ytitle(" " "Percent", axis(2)) 
			graph export "$finalpath/figures/asset_LatAm.pdf", replace
			
			twoway (line zRBT datem if geo == "Europe",  lcolor(gs5)) (line zR datem if geo == "Europe", yaxis(2) lcolor(black) lpattern(shortdash)), graphregion(color(white)) legend(label(1 "All US Equity Investors (l.h.s)") label(2 "EPFR (r.h.s)")) xtitle(" " "Months")  ytitle("Percent" " ") ytitle(" " "Percent", axis(2)) 
			graph export "$finalpath/figures/asset_Europe.pdf", replace

		
	restore 
}
* end
***************************************************************************